const {User} = require('../../model/');
const jwt = require('jsonwebtoken');
const md5 = require('blueimp-md5');


function auth(req, res) {
    const {username, password} = req.body;
    const secret = 'secret';

    User.findOne({username}, function (err, data) {
        if (data && md5(password) === data.password) {
            console.log('ok');
            const response = {
                meta: {
                    error: 0,
                },
                data: {
                    token: jwt.sign({username}, secret)
                }
            };
            console.log(response);
            return res.json(response);
        } else {
            return res.json({meta: {error: 1}})
        }
    })
}

module.exports = auth;
